/**
 * 功能:获取用户相关的信息
 * 创建日期: 2012-03-16
 * 创建人: 全轩
 * 功能修正说明:
 * 		
 */

if(!this.UserInfo)
{
	this.UserInfo = {};
}

(function(){
	
	/**
	 * 本类当中默认使用的数据表名称
	 */
	UserInfo.defaultTable = DataBase.tableNames.account;
	UserInfo.accountmappingTable = DataBase.tableNames.accountmapping;
	UserInfo.users_logoninfo = DataBase.tableNames.users_logoninfo;
	
	

	/**
	 * 跟用用户名称密码获取登陆信息
	 */
	if(typeof UserInfo.GetUserInfoByNameAndPassword !== 'function')
	{
		UserInfo.GetUserInfoByNameAndPassword = function(accountName,password)
		{
			return DAL.query("select * from "+ UserInfo.defaultTable +" where name='" + accountName + "' and password='" + Ti.Utils.md5HexDigest(accountPwd) + "'",['accountid','name','displayname']);
		}
	}

	
	/**
	 * 获取app默认的易动帐户
	 */
	if(typeof UserInfo.GetDefaultUser !== 'function')
	{
		UserInfo.GetDefaultUser = function()
		{
			var result  = DAL.Query('select * from ' + UserInfo.defaultTable  + ' where  isdefault = 1',['accountid','name','displayname','type','password','phone','address','company','teein_accountid','issync','create_at','logincount','lastlogintime']);
			
			if(result && result.length > 0)
			{
				return result[0];
			}
			else
			{
				return null;
			}
		}
	}
	
	
	/**
	 * 获取app默认的新浪帐户
	 */
	if(typeof UserInfo.GetDefaultSinaUserInfo !== 'function')
	{
		UserInfo.GetDefaultSinaUserInfo = function(uid)
		{
			var result  = DAL.Query('select * from ' + UserInfo.users_logoninfo  + ' where uid=' + uid ,['uid','screen_name','isdefault','istimeout','access_token','access_tokensecret','code','access_token_gettime','last_logontime','logon_count']);
			
			if(result && result.length > 0)
			{
				return result[0];
			}
			else
			{
				return null;
			}
		}
	}
	
	/**
	 * 获取app默认的新浪帐户
	 */
	if(typeof UserInfo.GetDefaultSinaUser !== 'function')
	{
		UserInfo.GetDefaultSinaUser = function(accountid)
		{
			var result  = DAL.Query('select * from ' + UserInfo.accountmappingTable  + ' where accountid=' + accountid + ' and isdefault = 1',['accountid','uid','isdefault']);

			if(result && result.length > 0)
			{
				return UserInfo.GetDefaultSinaUserInfo(result[0].uid);
			}
			else
			{
				return null;
			}
		}
	}
	
	/**
	 * 根据登录名和密码获取用户信息
	 */
	if(typeof UserInfo.GetUserByService !== 'function')
	{
		UserInfo.GetUserByService = function(accountname,password,callback)
		{
			
			var url = 'http://api.socialed.cn/UserService.asmx';
			url = 'http://192.168.1.6/Service/UserService.asmx';
			var data = '<?xml version="1.0" encoding="utf-8"?>'
				data += '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
				data += '<soap12:Body>'
				data += '<Login xmlns="http://www.socialed.cn/">'
				data += '<loginName>'+accountname+'</loginName>'
				data += '<loginPwd>'+ password+'</loginPwd>'
				data += '</Login>'
				data += '</soap12:Body>'
				data += '</soap12:Envelope>';
				SinaAuth.RequestWebServiceByPOST(url,data,function(responseText){

				var userinfo = UserInfo.AddUserInfo(responseText,accountname,password);
				Ti.API.info(userinfo);
				callback(userinfo);
				UserInfo.AddUserMappingInfo(userinfo.teein_parentid);
			})
			
			
		}
	}
	
	/**
	 * 更新远程登陆后的用户信息
	 */
	if(typeof UserInfo.UpdateUserInfo !== 'function')
	{
		UserInfo.UpdateUserInfo = function(responseText)
		{
			var result = JSON.parse(responseText);
			
			DAL.Update('update ' + UserInfo.defaultTable  + ' set password=\'' + result.Pwd + + '\ , logincount = logincount + 1, lastlogintime=\'' + new Date() + '\' where accountname=\''+result.accountname+'\'');
			
		}
	}
	
	/**
	 * 记录远程登陆的用户信息
	 */
	if(typeof UserInfo.AddUserInfo !== 'function')
	{
		UserInfo.AddUserInfo = function(responseText,accountname,password)
		{
			var doc = Titanium.XML.parseString(responseText);
			var result = JSON.parse(doc.getElementsByTagName("LoginResult").item(0).text);
			TeeinEDHandler.signature = result.Signature;
			var resultuser =  {};
			resultuser.accountid = result.UserID;
			resultuser.teein_parentid = result.ParentUserID;
			resultuser.name = accountname;
			resultuser.displayname = result.UserName;
			resultuser.type= '0';
			resultuser.password= password;
			resultuser.phone= '';
			resultuser.address= '';
			resultuser.company= '';
			resultuser.teein_accountid= result.UserID;
			resultuser.issync = '1';
			resultuser.create_at= new Date();
			resultuser.logincount= 1;
			resultuser.lastlogintime= new Date();
			resultuser.isdefault = 1;

			DAL.Add(UserInfo.defaultTable ,[resultuser],['accountid','name','displayname','isdefault','type','password','phone','address','company','teein_accountid','issync','create_at','logincount','lastlogintime']);
			
			return resultuser;
		}
	}
	
	if(typeof UserInfo.AddUserMappingInfo !== 'function')
	{
		UserInfo.AddUserMappingInfo = function(userinfo)
		{
			var url = 'http://api.socialed.cn/AccountService.asmx';
			url = 'http://192.168.1.6/Service/AccountService.asmx';
			var data = '<?xml version="1.0" encoding="utf-8"?>'
				data += '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
				data += '<GetAccountsByParentUserID xmlns="http://www.socialed.cn/">';
		      	data += '<userID>'+userinfo.accountid+'</userID>';
		      	data += '<signature>'+TeeinEDHandler.signature+'</signature>';
		      	data += '<parentUserID>'+userinfo.teein_parentid+'</parentUserID>';
			    data += '</GetAccountsByParentUserID>';
				data += '</soap12:Body>';
				data += '</soap12:Envelope>';
			Ti.API.info(data);
			SinaAuth.RequestWebServiceByPOST(url,data,function(responseText){
							var doc = Titanium.XML.parseString(responseText);
							var result = JSON.parse(doc.getElementsByTagName("GetAccountsByParentUserIDResult").item(0).text);
							
							var usermapping = {};
							usermapping.accountid = teein_parentid;
							usermapping.uid = '1971003933';
							usermapping.isdefault = 1;
							//DAL.Add(UserInfo.accountmappingTable,[usermapping],['accountid','uid','isdefault']);
			})
		}
	}
}())
//SinaApp.UserInfo = UserInfo;